<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Controllers\admin\CateController as CateController;
class GoodsController extends Controller
{
<<<<<<< HEAD
    //获取所有某个表所有数据
    public static function getData($table)
    {
        $data = DB :: table($table) ->get();
        return $data;
    }
    /**
    *获取分类下的商品
    */
    public static function getCate(Request $request)
    {
        $tid = $request -> input('tid');
        $data = DB :: table('shop_goods') -> where('tid', $tid) -> select('id', 'articlenum') -> get();
        echo json_encode($data);
    }
    /**
    *判断文件是否存在，并上传
    *
    *@param $pic varchar 上传文件名称
    *@param $request     接受数据的对象
    *@param $arr         要改变的数组
    *@param $path        上传文件存储路径
    *@return $arr        返回改变的数组
    */
    public static function uploadPic($pic, Request $request, $arr, $path="./uploads/images/goods")
    {   
        $ext = $request -> file($pic) -> getClientOriginalExtension();
        $arr[$pic] = time().rand(1000,9999).'.'.$ext;
        $request -> file($pic) -> move($path, $arr[$pic]); 
        return $arr;   
    }
=======
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
    /**
    *商品添加页面
    */
    public function getAdd()
    {
<<<<<<< HEAD
        //data是获取所有分类，data1是获取所有品牌,data2是获取所有价格范围,data3获取性别
        return view('/admin/goods/add',['data' => CateController::getCate(), 'data1' => self::getData('shop_brand'), 'data2' =>self::getData('shop_goods_pricerange'), 'data3' => self::getData('shop_goods_suitsex')]);
=======
        return view('/admin/goods/add',['data' => CateController::getCate()]);
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
    }
    /**
    *ajax验证用户所选分类必须是最精准的分类
    */
    public function getVerify(Request $request)
    {
        //获取库存表中的关联商品ID
        $tid = $request -> input('tid');
        $res = DB :: table('shop_cate') -> where('pid', $tid) -> first();
        if($res)
            echo 1;
    }
    /**
    *验证用户在商品库存添加时是否选择了商品
    */
    public function getInventoryverify(Request $request)
    {
        $gid = $request -> input('gid');
        $res = DB :: table('shop_goods') -> where('id', $gid) -> first();
        if($res)
            echo 1;
    }
    /**
    *执行商品添加
    */
    public function postInsert(Request $request)
    {
        $arr = $request -> except('_token');
        //验证字段是否符合要求
         $this -> validate($request,[
            'articlenum' => 'required | size : 6 |unique:shop_goods,articlenum',
            'goodsdes' => 'required',
            'goodsprice' => 'required | numeric',
            'tid' => 'integer',
            'pricerange' => 'required',
            'material' => 'required',
<<<<<<< HEAD
            'brand' => 'required',
            'gsex' => 'required'
=======
            'brand' => 'required'
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
        ],[
            'articlenum.required' => '货号不可为空',
            'articlenum.size' => '商品货号必须为6位',
            'articlenum.unique' => '输入的货号已存在',
            'goodsdes.required' => '商品描述不可为空',       
            'goodsprice.required' => '商品价格不可为空',
            'goodsprice.numeric' => '商品价格必须是数值类型',
            'tid.integer' => '商品未选择所属类别',
            'pricerange.required' => '商品价格范围未选',
            'material.required' => '商品材质未选',
<<<<<<< HEAD
            'brand.required' => '商品品牌未选',
            'gsex.required' => '商品适合性别未选'
        ]);
=======
            'brand.required' => '商品品牌未选'
        ]);
         //dd($arr);
        //如果有照片上传，将照片存放在uploads目录下
        /*if($request -> hasFile('goodspic')){
            $ext = $request -> file('goodspic') -> getClientOriginalExtension();
            $arr['goodspic'] = time().rand(1000,9999).'.'.$ext;
            $request -> file('goodspic') -> move('./uploads/images/goods/',$arr['goodspic']);
        }*/
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
        //执行insert语句
        $res = DB:: table('shop_goods') -> insert($arr);
        if($res){
            return redirect('/admin/goods/index') -> with('success', '商品添加成功');
        }else{
            return back() -> withInput() -> with('error', '商品添加失败');
        }
    }
    /**
    * 商品浏览页面
    */
    public function getIndex(Request $request)
    {
        $count = $request -> input('count',10);
        $search = $request -> input('search','');
        //带条件查询数据，并且分页
        $data = DB::table('shop_goods') ->join('shop_cate', 'shop_cate.id', '=', 'shop_goods.tid') ->select('shop_goods.id as id','articlenum','cname','goodsprice','goodsdes','status') ->where('articlenum','like','%'.$search.'%')->paginate($count);
        $page = $request -> input('page',1);
        $start = ($page - 1) * $count + 1;
        $sum = count(DB :: table('shop_goods') ->join('shop_cate', 'shop_cate.id', '=', 'shop_goods.tid') ->where('articlenum','like','%'.$search.'%')->get()); 
        $end = min($page * $count,$sum);
        return view('/admin/goods/index',['data'=>$data,'request'=>$request -> all(), 'start' => $start, 'end' => $end, 'sum' => $sum]);
    }
    /**
    *商品修改页面
    */
    public function getEdit($id)
    {
        $data1 = DB::table('shop_goods')->where('id',$id)->first();
<<<<<<< HEAD
        return view('/admin/goods/edit',['data' => CateController::getCate(), 'data1' => $data1,'data0' => self::getData('shop_brand'), 'data2' =>self::getData('shop_goods_pricerange'), 'data3' => self::getData('shop_goods_suitsex')]);
=======
        return view('/admin/goods/edit',['data'=>CateController::getCate(),'data1'=>$data1]);
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
    }
    /**
    *执行商品修改
    */
    public function postUpdate(Request $request)
    {
        //验证字段是否符合要求
<<<<<<< HEAD
        $arr = $request -> except('_token');
        //验证字段是否符合要求
         $this -> validate($request,[
=======
        $this -> validate($request,[
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
            'articlenum' => 'required | size : 6',
            'goodsdes' => 'required',
            'goodsprice' => 'required | numeric',
            'tid' => 'integer',
            'pricerange' => 'required',
            'material' => 'required',
<<<<<<< HEAD
            'brand' => 'required',
            'gsex' => 'required'
        ],[
            'articlenum.required' => '货号不可为空',
            'articlenum.size' => '商品货号必须为6位',
            'articlenum.unique' => '输入的货号已存在',
=======
            'brand' => 'required'
        ],[
            'articlenum.required' => '货号不可为空',
            'articlenum.size' => '商品货号必须为6位',
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
            'goodsdes.required' => '商品描述不可为空',       
            'goodsprice.required' => '商品价格不可为空',
            'goodsprice.numeric' => '商品价格必须是数值类型',
            'tid.integer' => '商品未选择所属类别',
            'pricerange.required' => '商品价格范围未选',
            'material.required' => '商品材质未选',
<<<<<<< HEAD
            'brand.required' => '商品品牌未选',
            'gsex.required' => '商品适合性别未选'
        ]);
        $arr = $request -> except(['_token','id']);
=======
            'brand.required' => '商品品牌未选'
        ]);
        $arr = $request -> except(['_token','id']);
        //判断是否修改了照片
        /*if($request -> hasFile('goodspic')){
            $ext = $request -> file('goodspic') -> getClientOriginalExtension();
            $arr['goodspic'] = time().rand(1000,9999).'.'.$ext;
            $request -> file('goodspic') -> move('./uploads/images/goods/',$arr['goodspic']);
        }*/
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
        $id = $request -> input('id');
        $res = DB::table('shop_goods') -> where('id', $id) -> update($arr);
        if($res >= 0){
            return redirect('/admin/goods/index') ->with('success','商品修改成功');
        }else{
            return back() -> withInput() -> with('error', '商品修改失败');
        } 

    }
    /**
    *执行商品删除
    */
    public function getDelete($id)
    {
        $res = DB::table('shop_goods')->where('id',$id)->delete();
        if($res)
            return redirect('/admin/goods/index')->with('success','商品删除成功');
        else
            return redirect('/admin/goods/index')->with('error','商品删除失败');
    }
    /**
    *添加商品照片页面
    */
    public function getPicadd()
    {
        return view('/admin/goods/picadd', ['data' => CateController::getCate()]);
    }
<<<<<<< HEAD

    /**
    *执行商品照片添加
    */
    public function postPicinsert(Request $request)
    {
        $arr = $request -> except(['_token', 'tid']);
        
        foreach($arr as $k => $v)
        {
            if(preg_match("/^goodspic[\d]$/",$k,$match)){
                $arr = self::uploadPic($k, $request, $arr);
                
            }  
        }
        $res = DB :: table('shop_goodspic') ->insert($arr);
        if($res)
            return redirect('/admin/goods/picindex') ->with('success', '商品照片添加成功');
        else
            return back() -> withInput() -> with('error', '商品照片添加失败');
    }

    /**
    *浏览商品照片
    */
    public function getPicindex(Request $request)
    {
        $count = $request -> input('count',10);
        $search = $request -> input('search','');
        //带条件查询数据，并且分页
        $data = DB::table('shop_goodspic') ->join('shop_goods', 'shop_goodspic.gid', '=', 'shop_goods.id') ->join('shop_cate', 'shop_cate.id', '=', 'shop_goods.tid') ->select('shop_goodspic.id as id','cname','articlenum','goodspic1','goodspic2','goodspic3','goodspic4','goodspic5') ->where('cname','like','%'.$search.'%')->paginate($count);
        $page = $request -> input('page',1);
        $start = ($page - 1) * $count + 1;
        $sum = count(DB::table('shop_goodspic') ->join('shop_goods', 'shop_goodspic.gid', '=', 'shop_goods.id') ->join('shop_cate', 'shop_cate.id', '=', 'shop_goods.tid') ->where('cname','like','%'.$search.'%')->get()); 
        $end = min($page * $count,$sum);
        return view('/admin/goods/picindex',['data'=>$data,'request'=>$request -> all(), 'start' => $start, 'end' => $end, 'sum' => $sum]);
    }
    /**
    *修改商品照片页面
    */
    public function getPicedit($id)
    {
        $data = DB :: table('shop_goods') ->join('shop_goodspic', 'shop_goods.id', '=', 'shop_goodspic.gid') ->select('shop_goodspic.id as id', 'articlenum','goodspic1', 'goodspic2', 'goodspic3', 'goodspic4', 'goodspic5') ->where('shop_goodspic.id', $id) ->first();
        return view('/admin/goods/picedit', ['data' => $data]);
    }
    /**
    *执行商品照片修改
    */
    public function postPicupdate(Request $request)
    {
        $arr = $request -> except(['_token', 'id']);
        $id = $request -> input('id');
        foreach($arr as $k => $v)
        {
            if(preg_match("/^goodspic[\d]$/",$k,$match)){
                $arr = self::uploadPic($k, $request, $arr);        
            }       
        }
        $res = DB :: table('shop_goodspic') -> where('id', $id) ->update($arr);
        if($res)
            return redirect('/admin/goods/picindex') ->with('success', '商品照片修改成功');
        else
            return back() -> withInput() -> with('error', '商品照片修改失败');
    }
=======
    /**
    *修改商品照片页面
    */
    /**
    *执行商品添加
    */
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
    /**
    *商品库存展示页面
    */
    public function getInventory(Request $request)
    {
        $count = $request -> input('count',10);
        $search = $request -> input('search','');
        //带条件查询数据，并且分页
<<<<<<< HEAD
        $data = DB::table('shop_goods')->join('shop_goodsinventory','shop_goods.id','=','shop_goodsinventory.gid') ->join('shop_goods_color', 'shop_goods_color.id', '=', 'shop_goodsinventory.color') ->join('shop_goods_size', 'shop_goods_size.id', '=', 'shop_goodsinventory.size')-> select('shop_goodsinventory.id as id', 'articlenum', 'sizename', 'colorname', 'num') ->where('shop_goods.articlenum','like','%'.$search.'%') ->orderBy('shop_goodsinventory.id', 'asc') ->paginate($count);
        $page = $request -> input('page',1);
        $start = ($page - 1) * $count + 1;
        $sum = count(DB::table('shop_goods')->join('shop_goodsinventory','shop_goods.id','=','shop_goodsinventory.gid')->where('shop_goods.articlenum','like','%'.$search.'%') ->get()); 
=======
        $data = DB::table('shop_goods')->join('shop_goodsinventory','shop_goods.id','=','shop_goodsinventory.gid') -> where('shop_goods.articlenum','like','%'.$search.'%')->paginate($count);
        $page = $request -> input('page',1);
        $start = ($page - 1) * $count + 1;
        $sum = count(DB::table('shop_goods')->join('shop_goodsinventory','shop_goods.id','=','shop_goodsinventory.gid')->get()); 
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
        $end = min($page * $count,$sum);
        return view('/admin/goods/inventoryindex',['data'=>$data,'request'=>$request -> all(),'start' => $start, 'end' => $end, 'sum' => $sum]);
    }
    /**
    *获取所有商品
    */
    static public function getGoods()
    {
        $data = DB::table('shop_goods') -> select('id', 'articlenum') -> get();
        return $data;
    }
    /**
    *商品库存添加页面
    */
    public function getInventoryadd()
    { 
<<<<<<< HEAD
        return view('/admin/goods/inventoryadd', ['data' => self::getGoods(), 'data1' => self::getData('shop_goods_size'), 'data2' => self::getData('shop_goods_color')]);
=======
        return view('/admin/goods/inventoryadd', ['data' => self::getGoods()]);
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
    }
    /**
    *执行商品库存添加
    */
    public function postInventoryinsert(Request $request)
    {
        $arr = $request -> except(['_token']);
        $res = DB::table('shop_goodsinventory') -> insert($arr);
        if($res){
            return redirect('/admin/goods/inventory') ->with('success','商品库存添加成功');
        }else{
            return back() -> withInput() -> with('error', '商品库存添加失败');
        } 
    }
    /**
    *商品库存修改页面
    */
    public function getInventoryedit($id)
    {
        $data1 = DB::table('shop_goodsinventory') -> where('id', $id) -> first();
        $data = DB::table('shop_goods') -> where('id',$data1['gid']) -> first();
<<<<<<< HEAD
        return view('/admin/goods/inventoryedit', ['data' => $data['articlenum'], 'data3' => self::getData('shop_goods_size'), 'data2' => self::getData('shop_goods_color'), 'data1' =>$data1]);
=======
        return view('/admin/goods/inventoryedit', ['data' => $data['articlenum'], 'data1' =>$data1]);
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
    }
    /**
    *执行商品库存修改
    */
    public function postInventoryupdate(Request $request)
    {
        $arr = $request ->except(['_token', 'id']);
        $id = $request -> input('id');
        $res = DB::table('shop_goodsinventory') -> where('id',$id) ->update($arr);
        if($res)
            return redirect('/admin/goods/inventory') -> with('success','商品库存修改成功');
        else
            return back() -> withInput() -> with('error', '商品库存修改失败或未修改');
    }
    /**
    *执行商品库存删除
    */
    public function getInventorydelete($id)
    {
        $res = DB::table('shop_goodsinventory') -> where('id',$id) -> delete();
        if($res)
            return redirect('/admin/goods/inventory') -> with('success','商品库存删除成功');
        else
            return back() -> withInput() -> with('error', '商品库存删除失败');
    }
<<<<<<< HEAD
    /**
    *促销商品完善信息页面
    */
    public function getDepreciateadd()
    {
        $data = DB :: table('shop_goods') ->select('id', 'goodsprice', 'articlenum') ->where('status', 2)->get();
        return view('/admin/goods/depreciateadd', ['data' => $data]);
    }
    /**
    *执行促销信息添加
    */
    public function postDepreciateinsert(Request $request)
    {
        $arr = $request ->except(['_token', 'starttime', 'expiretime']);
        $arr['starttime'] = strtotime($request ->input('starttime'));
        $arr['expiretime'] = strtotime($request ->input('expiretime'));
        $res = DB :: table('shop_goods_depreciate') ->insert($arr);
        if($res){
            return redirect('/admin/goods/depreciateindex') ->with('success','促销商品完善成功');
        }else{
            return back() -> withInput() -> with('error', '促销商品完善失败');
        } 
    }
    /**
    *显示促销信息
    */
    public function getDepreciateindex(Request $request)
    {
        $count = $request -> input('count',10);
        $search = $request -> input('search','');
        //带条件查询数据，并且分页
        $data = DB::table('shop_goods_depreciate')->join('shop_goods','shop_goods.id','=','shop_goods_depreciate.gid') ->select('shop_goods_depreciate.id as id', 'gid', 'articlenum', 'goodsprice', 'goodsdeprice', 'theme', 'slogan', 'starttime', 'expiretime') ->where('shop_goods.articlenum','like','%'.$search.'%')->paginate($count);
        $page = $request -> input('page',1);
        $start = $data ? (($page - 1) * $count + 1) : 0;
        $sum = count(DB::table('shop_goods_depreciate')->join('shop_goods','shop_goods.id','=','shop_goods_depreciate.gid') ->where('shop_goods.articlenum','like','%'.$search.'%')->get()); 
        $end = min($page * $count,$sum);
        return view('/admin/goods/depreciateindex',['data'=>$data,'request'=>$request -> all(),'start' => $start, 'end' => $end, 'sum' => $sum]);
    }
    /**
    *修改促销信息页面
    */
    public function getDepreciateedit($id)
    {
        $data = DB :: table('shop_goods_depreciate') ->join('shop_goods', 'shop_goods.id', '=', 'shop_goods_depreciate.gid') ->select('shop_goods_depreciate.id as id', 'gid', 'goodsdeprice', 'theme', 'slogan', 'starttime', 'expiretime')->where('shop_goods_depreciate.id', $id) ->first();
        $data1 = DB :: table('shop_goods') ->select('id', 'articlenum') ->where('status', 2)->get();
        //dd($data1);
        return view('/admin/goods/depreciateedit', ['data' =>$data, 'data1' => $data1]); 
    }
    /**
    *执行促销商品信息修改
    */
    public function postDepreciateupdate(Request $request)
    {
        $arr = $request ->except(['_token', 'id']);
        $arr['starttime'] = strtotime($arr['starttime']);
        $arr['expiretime'] = strtotime($arr['expiretime']);
        $id = $request ->input('id');
        $res = DB :: table('shop_goods_depreciate') ->where('id', $id) ->update($arr);
        if($res){
            return redirect('/admin/goods/depreciateindex') ->with('success','促销商品修改成功');
        }else{
            return back() -> withInput() -> with('error', '促销商品修改失败');
        } 
    }
    /**
    *执行促销信息删除
    */
    public function getDepreciatedelete($id)
    {
        $res = DB :: table('shop_goods_depreciate') ->where('id', $id) ->delete();
        if($res){
            return redirect('/admin/goods/depreciateindex') ->with('success','促销商品删除成功');
        }else{
            return back() -> withInput() -> with('error', '促销商品删除失败');
        } 
    }
=======
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
}
